package org.jash.demo2205.entity

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable

class Cart : BaseObservable() {
    val data: MutableList<CartItem> = mutableListOf()

    @get:Bindable("allChecked")
    val total: Float
        get() = data.filter(CartItem::checked).map { it.goods_default_price * it.count }.sum()

    @get:Bindable("allChecked")
    var allChecked: Int
        get() = when (data.filter(CartItem::checked).size) {
            0 -> 0
            data.size -> 1
            else -> 2
        }
        set(value) {
            if (value != 2) {
                data.forEach { it.checked = value == 1 }
            }
        }
}